package org.linlinjava.litemall.admin.controller.order;

import java.util.List;

import javax.validation.constraints.NotNull;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.annotation.LoginAdminShopId;
import org.linlinjava.litemall.admin.controller.order.vo.AdminSalesStatisticsReqVO;
import org.linlinjava.litemall.admin.controller.order.vo.AdminShopOrderDayStatisticsPageReqVO;
import org.linlinjava.litemall.admin.service.AdminOrderService;
import org.linlinjava.litemall.admin.vo.goodsstatistics.GoodsStatisticReqVO;
import org.linlinjava.litemall.admin.vo.order.AdminLumiereOrderPageRespVO;
import org.linlinjava.litemall.admin.vo.order.OrderDayDetailReqVO;
import org.linlinjava.litemall.admin.vo.order.OrderTransactionDataReqVO;
import org.linlinjava.litemall.admin.vo.orderstatistics.HourStatisticsLineReqVO;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.ResponseUtil.ListResponse;
import org.linlinjava.litemall.core.util.ResponseUtil.Response;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallOrderRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "Admin订单API")
@RestController
@RequestMapping("/admin/order")
@Validated
public class AdminOrderController {

    @Autowired
    private AdminOrderService adminOrderService;

    @ApiOperation("admin-「订单列表」")
    @PostMapping("/list")
    @LogAnno
    public ResponseUtil.Response list(@RequestBody AdminLumiereOrderPageRespVO respVO) {
        return adminOrderService.list(respVO);
    }

    /**
     * 会员订单记录
     *
     * @param userName         用户名
     * @param userId           用户id
     * @param orderSn          订单sn
     * @param id               id
     * @param shopId           商店id
     * @param orderStatusArray 订单状态数组
     * @param orderType        订单类型
     * @param page             页面
     * @param limit            限制
     * @param sort             排序
     * @param order            订单
     * @return {@link Object}
     */
    @GetMapping("/listInMember")
    @LogAnno
    public Object listInMember(String userName, Integer userId, String orderSn,Integer id,
                       @LoginAdminShopId Integer shopId,
                       @RequestParam(required = false) List<Short> orderStatusArray,
                       @RequestParam(required = false) Byte orderType,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        return adminOrderService.listInMember(userName, id, userId, orderSn, orderStatusArray, orderType, shopId, page, limit, sort, order);
    }


    @ApiOperation("会员详情订单记录")
    @GetMapping("userOrderMemberList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户ID"),
            @ApiImplicitParam(name = "page",value = "Page"),
            @ApiImplicitParam(name = "limit",value = "Limit")
    })
    @ApiImplicitParam(name = "userId",value = "用户ID")
    @LogAnno
    public Response userOrderMemberList(@Validated @NotNull @RequestParam("userId")Integer userId
            ,@RequestParam("page") Integer page
            ,@RequestParam("limit")Integer limit){
        return adminOrderService.userOrderMemberList(userId,page,limit);
    }

    /**
     * 细节
     * 详情
     *
     * @param id id
     * @return {@link Object}
     */
    @GetMapping("/detail")
    @LogAnno
    public Object detail(@NotNull @RequestParam(value = "id") Integer id) {
        return adminOrderService.detail(id);
    }

    /**
     * 退款
     * 订单退款
     *
     * @param body   订单Id，{ orderId：xxx }
     * @param shopId 商店id
     * @return 订单退款操作结果
     */
    @PostMapping("/refund")
    @LogAnno
    public Object refund(@RequestBody String body, @LoginAdminShopId Integer shopId) {
        Integer orderId = JacksonUtil.parseInteger(body, "orderId");
        if(orderId == null){
            return ResponseUtil.badArgument();
        }
        return adminOrderService.refund(orderId, shopId);
    }

    /**
     * 船
     * 发货
     *
     * @param body 订单信息，{ orderId：xxx, shipSn: xxx, shipChannel: xxx }
     * @return 订单操作结果
     */
    @PostMapping("/ship")
    @LogAnno
    public Object ship(@RequestBody String body) {
        return adminOrderService.ship(body);
    }


    /**
     * 回复
     * 回复订单商品
     *
     * @param body 订单信息，{ orderId：xxx }
     * @return 订单操作结果
     */
    @PostMapping("/reply")
    @LogAnno
    public Object reply(@RequestBody String body) {
        return adminOrderService.reply(body);
    }

    /**
     * 马克
     * 添加订单备注
     *
     * @param body   订单信息，{ orderId：xxx }
     * @param shopId 商店id
     * @return 订单操作结果
     */
    @PostMapping("/remark")
    @LogAnno
    public Object mark(@RequestBody String body, @LoginAdminShopId Integer shopId) {
        return adminOrderService.remark(body, shopId);
    }

    /**
     * 商品统计
     *
     * @param shopId    商店id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return {@link Object}
     */
    @GetMapping("/goodsStatistics")
    @LogAnno
    public Object goodsStatistics(@LoginAdminShopId Integer shopId,@NotNull @RequestParam(value = "startTime") String startTime,@NotNull @RequestParam(value = "endTime")  String endTime){
        return adminOrderService.goodsStatistics(startTime, endTime, shopId);
    }


    @ApiOperation("商品销售情况 概况")
    @PostMapping("/goodsSalesStatisticsGeneral")
    @LogAnno
    public Object goodsSalesStatisticsGeneral(@Validated @RequestBody GoodsStatisticReqVO reqVO){
        return adminOrderService.goodsSalesStatisticsGeneral(reqVO);
    }
    @ApiOperation("商品销售情况")
    @PostMapping("/goodsSalesStatistics")
    @LogAnno
    public Object goodsSalesStatistics(@Validated @RequestBody GoodsStatisticReqVO reqVO){
        return adminOrderService.goodsSalesStatistics(reqVO);
    }



    @ApiOperation("商品统计-「商品浏览量情况」")
    @GetMapping("/goodsBrowser")
    @LogAnno
    public Object goodsBrowser(){
        return adminOrderService.goodsBrowser();
    }



    @ApiOperation("销售统计")
    @GetMapping("/salesStatistics")
    @LogAnno
    public Object salesStatistics(AdminSalesStatisticsReqVO reqVO){
        return adminOrderService.salesStatistics(reqVO);
    }


    @PostMapping("/transactionData")
    @LogAnno
    public Object transactionData(@Validated @RequestBody OrderTransactionDataReqVO reqVO){
        return adminOrderService.transactionData(reqVO);
    }


    /**
     * 查询订单完成id
     * 根据订单ID 查询订单是否完成 或者已评价 1：已完成 2：已评价 3：未完成
     *
     * @param orderId 订单id
     * @return {@link Object}
     */
    @GetMapping("/queryOrderIsCompletionById")
    @LogAnno
    public Object queryOrderIsCompletionById(@NotNull @RequestParam(value = "orderId") Integer orderId){
        return adminOrderService.queryOrderIsCompletionById(orderId);
    }

    /**
     * 收货人
     * 修改订单收货信息
     *
     * @param body   订单信息，{ orderId：xxx }
     * @param shopId 商店id
     * @return 订单操作结果
     */
    @PutMapping("/consignee")
    @LogAnno
    public Object consignee(@RequestBody String body, @LoginAdminShopId Integer shopId) {
        return adminOrderService.consignee(body, shopId);
    }

    /**
     * 订单月统计数据
     * 订单按月统计查询
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param regionId  区域id
     * @param shopId    商店id
     * @param page      页面
     * @param limit     限制
     * @return {@link Object}
     */
    @GetMapping("/order_month_statistics")
    @LogAnno
    public Object orderMonthStatistics(String startTime, String endTime, Integer regionId,
    		@LoginAdminShopId Integer shopId,
    		@RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {
        return adminOrderService.orderMonthStatistics(startTime, endTime, regionId, shopId, page, limit);
    }

    @ApiOperation("订单按日统计查询")
    @GetMapping("/order_day_statistics")
    @LogAnno
    public Object orderDayStatistics(@LoginAdminShopId Integer shopId, AdminShopOrderDayStatisticsPageReqVO pageReqVO) {
        pageReqVO.setShopId(shopId);
    	return adminOrderService.orderDayStatistics(pageReqVO);
    }


    @ApiOperation("订单小时统计")
    @GetMapping("/order_hour_statistics")
    @LogAnno
    public Object orderHourStatistics(String searchDate,
    		@LoginAdminShopId Integer shopId,
    		@RequestParam(defaultValue = "1") Integer page,
    		@RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(name = "time",defaultValue = "15") Integer time) {
    	return adminOrderService.orderHourStatistics(searchDate, time, shopId, page, limit);
    }


    @ApiOperation("小时对比折线图")
    @PostMapping("/order_hour_statistics_line")
    @LogAnno
    public Object orderHourStatisticsLine(@Validated @RequestBody HourStatisticsLineReqVO reqVO) {
     /*   reqVO.setTime(15);
        reqVO.setShopId(145);
        reqVO.setSearchDateList(Arrays.asList("2024-11-30","2024-12-01","2024-12-02"));*/
        return adminOrderService.orderHourStatisticsLine(reqVO);
    }


    @ApiOperation("店铺订单按日统计查询")
    @GetMapping("/shop_order_day_statistics")
    @LogAnno
    public ResponseUtil.Response shopOrderDayStatistics(@LoginAdminShopId Integer shopId, AdminShopOrderDayStatisticsPageReqVO pageReqVO) {
        pageReqVO.setShopId(shopId);
    	return adminOrderService.orderDayStatistics(pageReqVO);
    }


    @ApiOperation("订单按日统计详情")
    @PostMapping("/order_day_detail")
    @LogAnno
    public Object orderDayDetail(@RequestBody OrderDayDetailReqVO reqVO) {
    	return adminOrderService.orderDayStatisticsDetail(reqVO);
    }

    /**
     * 订单总额统计
     * 订单按月统计查询
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param regionId  区域id
     * @param shopId    商店id
     * @param page      页面
     * @param limit     限制
     * @return {@link Object}
     */
    @GetMapping("/order_total_statistics")
    @LogAnno
    public Object orderTotalStatistics(String startTime, String endTime, Integer regionId,
    		@LoginAdminShopId Integer shopId,
    		@RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {
        return adminOrderService.orderTotalStatistics(startTime, endTime, regionId, shopId);
    }


    /**
     * 订单类型统计
     * 订单统计
     * 订单按 付款方式、门店、线上、线下下单 统计查询
     *
     * @param payType     支付类型 (1:现金支付,2:礼物卡支付,3:Paypal支付,4:余额支付,5:Clover支付,6:积分兑换)
     * @param shopId      商店id
     * @param orderSource 订单来源 1:手机app下单,2:门店下单,3:官网商城,4:订阅
     * @param page        页面
     * @param limit       限制
     * @return {@link ResponseUtil.Response}
     */
    @ApiOperation(value = "订单类型统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "payType",value = "支付类型 (1:现金支付,2:礼物卡支付,3:Paypal支付,4:余额支付,5:Clover支付,6:积分兑换)",required = false),
            @ApiImplicitParam(name = "shopId",value = "商店ID",required = false),
            @ApiImplicitParam(name = "orderSource",value = "订单来源 1:手机app下单,2:门店下单,3:官网商城,4:订阅",required = false)
    })
    @GetMapping(value = "/orderTypeStatistics")
    @LogAnno
    public ResponseUtil.Response orderTypeStatistics(Integer payType, Integer shopId, Integer orderSource,
                                                     @RequestParam(defaultValue = "1") Integer page,
                                                     @RequestParam(defaultValue = "10") Integer limit){
        return adminOrderService.orderTypeStatistics(payType,shopId,orderSource,page,limit);
    }





    /**
     * 得到评论
     *
     * @param orderId 订单id
     * @return {@link Response}<{@link ListResponse}<{@link LitemallOrderRecord}>>
     */
    @GetMapping("/get_remarks")
    @LogAnno
    public Response<ListResponse<LitemallOrderRecord>> getRemarks(@RequestParam Integer orderId) {
         List<LitemallOrderRecord> litemallOrderRecords = adminOrderService.getRemarks(orderId);
         return ResponseUtil.okList(litemallOrderRecords);
    }

}
